#include "glViewer.h"


glViewer::glViewer(void)
{
}


glViewer::~glViewer(void)
{
}

void glViewer::initializeWindow(int width, int height, const char * title)
{
	if (!glfwInit())
		exit(EXIT_FAILURE);
	window = glfwCreateWindow(width,height, title, NULL, NULL);
	WIDTH=width;
	HEIGHT=height;
	if (!window)
	{
		glfwTerminate();
		exit(EXIT_FAILURE);
	}
	
	glfwMakeContextCurrent(window);

	glfwSwapInterval(4);
	setupTextureForNI();
	// Camera setup
	glViewport(0, 0, WIDTH, HEIGHT);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(0, WIDTH, HEIGHT, 0, 1, -1);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}

void glViewer::setupTextureForNI()
{
	glewInit();
	// Initialize color textures
	glGenTextures(1, &colorTex);
	glBindTexture(GL_TEXTURE_2D, colorTex);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, WIDTH, HEIGHT,
		0, GL_RGB, GL_UNSIGNED_BYTE, colorData);
	glBindTexture(GL_TEXTURE_2D, 0);

	// Initialize depth textures
	glGenTextures(1, &depthTex);
	glBindTexture(GL_TEXTURE_2D, depthTex);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, WIDTH, HEIGHT,
		0, GL_LUMINANCE, GL_UNSIGNED_SHORT, depthData);
	glBindTexture(GL_TEXTURE_2D, 0);

	// OpenGL setup
	glClearColor(0,0,0,0);
	glClearDepth(1.0f);

	glDisable(GL_DEPTH_TEST);
	glEnable(GL_TEXTURE_2D);

	glEnableClientState(GL_VERTEX_ARRAY);
	//glDisableClientState(GL_COLOR_ARRAY);
}

void glViewer::setupTextureForMS()
{
	// Initialize color textures
	glGenTextures(1, &colorTex);
	glBindTexture(GL_TEXTURE_2D, colorTex);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, WIDTH, HEIGHT,
		0, GL_RGBA, GL_UNSIGNED_BYTE, colorData);
	glBindTexture(GL_TEXTURE_2D, 0);

	// Initialize depth textures
	glGenTextures(1, &depthTex);
	glBindTexture(GL_TEXTURE_2D, depthTex);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, WIDTH, HEIGHT,
		0, GL_LUMINANCE, GL_UNSIGNED_SHORT, depthData);
	glBindTexture(GL_TEXTURE_2D, 0);

	// OpenGL setup
	glClearColor(0,0,0,0);
	glClearDepth(1.0f);

	glDisable(GL_DEPTH_TEST);
	glEnable(GL_TEXTURE_2D);

	//glEnableClientState(GL_VERTEX_ARRAY);
	//glDisableClientState(GL_COLOR_ARRAY);
}

void glViewer::rendering()
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glColor3f(1,1,1);
	if (drawDepth)
	{
		glBindTexture(GL_TEXTURE_2D, depthTex);
		glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, WIDTH, HEIGHT, GL_LUMINANCE, GL_UNSIGNED_SHORT, depthData);
	}
	else
	{
		glBindTexture(GL_TEXTURE_2D, colorTex);
		glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, WIDTH, HEIGHT, GL_RGB, GL_UNSIGNED_BYTE, colorData);
	}

	glEnable(GL_TEXTURE_2D);
	glBegin(GL_QUADS);
		glTexCoord2f(0.0f, 0.0f);glVertex2f(0, 0);
		glTexCoord2f(1.0f, 0.0f);glVertex2f(640, 0);
		glTexCoord2f(1.0f, 1.0f);glVertex2f(640, 480);
		glTexCoord2f(0.0f, 1.0f);glVertex2f(0, 480);
	glEnd();
	glDisable(GL_TEXTURE_2D);

	if (handData)
	{
		//printf("hand point %f - %f - %f \n",handData[0],handData[1],handData[2]);
		glColor3f(1,0,0);
		glPointSize(12);
		glVertexPointer(3, GL_FLOAT, 0, handData);
		glDrawArrays(GL_POINTS, 0, 1);
	}

	glfwSwapBuffers(window);
	glfwPollEvents();
}